import {
  getGroupTreeList,
  queryIdGroupData,
  querySelectDataGroupData,
} from "@/api/centre";
import { initChildren, organizerTreeKey } from "@/utils/initDiGuiFn.js";
/**
 * TreeData 左边Tree数据
 * TreeNode 左边Tree选中节点数据
 * TableData 右边Table展示数据
 * TableItem 右边Table选中数据
 * selectData 右边上面筛选的数据
 * NodeAndQueryFnSwitch 一个开关 因为左边选节点和右上筛选调的接口不一 故需开关控制调那个接口
 * pageFrom 分页器
 * pagination 分页器
 */
export default {
  state: {
    TreeData: Array(),
    TreeNode: Object(),
    TableData: Array(),
    TableItem: Object(),
    selectData: Object(),
    NodeAndQueryFnSwitch: String("node"), //query 2个状态
    pageFrom: {
      pageNum: Number(1),
      pageSize: Number(10),
    },
    pagination: {
      total: Number(),
      showSizeChanger: Boolean(true),
    },
  },
  mutations: {
    SET_TREE_DATA(state, data) {
      state.TreeData = data;
    },
    SET_TREE_NODE(state, data) {
      state.TreeNode = data;
    },
    SET_TABLE_DATA(state, data) {
      state.TableData = data;
    },
    SET_TABLE_ITEM(state, data) {
      state.TableItem = data;
    },
    SET_SELECT_DATA(state, data) {
      state.selectData = data;
    },
    SET_PAGE_FROM(state, data) {
      state.pageFrom = data;
    },
    SET_PAGE_TOTAL(state, data) {
      state.pagination.total = Number(data);
    },
    SET_SWITCH(state, data) {
      state.NodeAndQueryFnSwitch = data;
    },
  },
  actions: {
    GetGroupTreeList({ commit }) {
      getGroupTreeList().then((res) => {
        const { result } = res.data;
        initChildren(result, organizerTreeKey);
        commit("SET_TREE_DATA", result);
      });
    },
    SetGroupTreeNode(context, data) {
      const dataX = data || context.state.TreeNode;
      context.commit("SET_TREE_NODE", dataX);
      queryIdGroupData(dataX).then((res) => {
        context.commit("SET_SWITCH", "node");
        if (res.data.result) {
          context.commit("SET_TABLE_DATA", [res.data.result]);
        } else {
          context.commit("SET_TABLE_DATA", undefined);
        }
      });
    },
    GetGroupTableList2({ state, commit }) {
      let form = { ...state.pageFrom, ...state.selectData };
      querySelectDataGroupData(form).then((res) => {
        const { list, total } = res.data.result;
        commit("SET_SWITCH", "query");
        commit("SET_TABLE_DATA", list);
        commit("SET_PAGE_TOTAL", total);
      });
    },
  },
  getters: {
    xTreeData(state) {
      return state.TreeData;
    },
  },
};
